package com.jiumi.baseconfig.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.Random;

import com.jiumi.common.constant.UserConstants;
import com.jiumi.common.utils.DateUtils;
import com.jiumi.common.utils.StringUtils;
import com.jiumi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiumi.baseconfig.mapper.BaseUserMapper;
import com.jiumi.baseconfig.domain.BaseUser;
import com.jiumi.baseconfig.service.IBaseUserService;

/**
 * 用户信息Service业务层处理
 *
 * @author jiumi
 * @date 2023-05-21
 */
@Service
public class BaseUserServiceImpl implements IBaseUserService
{
    @Autowired
    private BaseUserMapper baseUserMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询用户信息
     *
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public BaseUser selectBaseUserByUserId(Long userId)
    {
        return baseUserMapper.selectBaseUserByUserId(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param baseUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<BaseUser> selectBaseUserList(BaseUser baseUser)
    {
        return baseUserMapper.selectBaseUserList(baseUser);
    }
    @Override
    public String getUserNumber() {
        String userNumber = genUserNumber();
        boolean flag = true;
        while (flag) {
            if (checkUserNumberUniq(userNumber)) {
                return userNumber;
            } else {
                genUserNumber();
            }
        }
        return userNumber;
    }

    public String genUserNumber() {

//        String timeSS = DateUtils.getTimeSS();
        int random=new Random().nextInt(9000)+1000;
        String userPrefix = sysConfigService.selectConfigByKey("user_prefix");
        return userPrefix+random;

    }


    public boolean checkUserNumberUniq(String userNumber) {
        BaseUser userInfo = selectBaseUserByUserName(userNumber);
        if (null == userInfo) {
            return true;
        }
        return false;
    }

    /**
     * 新增用户信息
     *
     * @param baseUser 用户信息
     * @return 结果
     */
    @Override
    public int insertBaseUser(BaseUser baseUser)
    {
        baseUser.setCreateTime(DateUtils.getNowDate());
        return baseUserMapper.insertBaseUser(baseUser);
    }

    /**
     * 修改用户信息
     *
     * @param baseUser 用户信息
     * @return 结果
     */
    @Override
    public int updateBaseUser(BaseUser baseUser)
    {
        baseUser.setUpdateTime(DateUtils.getNowDate());
        return baseUserMapper.updateBaseUser(baseUser);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseUserByUserIds(Long[] userIds)
    {
        return baseUserMapper.deleteBaseUserByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseUserByUserId(Long userId)
    {
        return baseUserMapper.deleteBaseUserByUserId(userId);
    }

    @Override
    public BaseUser selectBaseUserByUserName(String username) {
        return baseUserMapper.selectBaseUserByUserName(username);
    }

    @Override
    public int resetUserPwdByPhone(String userName, String password) {
        return baseUserMapper.resetUserPwdByPhone(userName,password);
    }

    @Override
    public String checkPhoneUnique(BaseUser user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        BaseUser info = baseUserMapper.checkPhoneUnique(user.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public long selectUserNameNotEmptyCount() {
        return baseUserMapper.selectUserNameNotEmptyCount();
    }

    @Override
    public BaseUser selectBaseUserByUserPhone(String userPhone) {
        return baseUserMapper.selectBaseUserByUserPhone(userPhone);
    }

    @Override
    public long selectUserNameMaxNo() {
        return baseUserMapper.selectUserNameMaxNo();
    }

    @Override
    public String checkAuthName(BaseUser baseUser) {
        Long userId = StringUtils.isNull(baseUser.getUserId()) ? -1L : baseUser.getUserId();
        BaseUser info = baseUserMapper.checkAuthNameUnique(baseUser.getAuthName());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
